package com.bookcloud.java21.chapter5.实参传递分析53;

public class ArgumentPassingAnalysis {

  // 基本类型参数
  public static void modifyPrimitive(int value) {
    value = 100;
    System.out.println("Inside modifyPrimitive: value = " + value);
  }

  // 对象引用参数 - 修改对象状态
  public static void modifyObject(StringBuilder builder) {
    builder.append(" World");
    System.out.println("Inside modifyObject: builder = " + builder);
  }

  // 对象引用参数 - 重新分配引用
  public static void reassignObject(StringBuilder builder) {
    builder = new StringBuilder("New StringBuilder");
    System.out.println("Inside reassignObject: builder = " + builder);
  }

  // 不可变对象参数
  public static void modifyString(String str) {
    str = str + " World";
    System.out.println("Inside modifyString: str = " + str);
  }

  // 数组参数 - 数组是对象
  public static void modifyArray(int[] arr) {
    arr[0] = 100;
    System.out.print("Inside modifyArray: arr = ");
    for (int num : arr) {
      System.out.print(num + " ");
    }
    System.out.println();
  }

  public static void main(String[] args) {
    // 基本类型测试
    int num = 10;
    System.out.println("Before modifyPrimitive: num = " + num);
    modifyPrimitive(num);
    System.out.println("After modifyPrimitive: num = " + num);
    // 可变对象测试
    StringBuilder sb = new StringBuilder("Hello");
    System.out.println("\nBefore modifyObject: sb = " + sb);
    modifyObject(sb);
    System.out.println("After modifyObject: sb = " + sb);

    // 数组测试
    int[] array = {1, 2, 3, 4, 5};
    System.out.print("\nBefore modifyArray: array = ");
    for (int n : array) {
      System.out.print(n + " ");
    }
    System.out.println();

    modifyArray(array);

    System.out.print("After modifyArray: array = ");
    for (int n : array) {
      System.out.print(n + " ");
    }
    System.out.println();
  }
}
